<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Constants\RoomTaskCenterTypeConstants;
use App\CustHub\Dto\RoomTcContentDto;
use App\CustHub\Mapper\ChRoomAutoTagConfigMapper;
use App\CustHub\Mapper\ChRoomMapper;
use App\CustHub\Model\ChRoomAutoTagConfig;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\Di\Annotation\Inject;
use Mine\Abstracts\AbstractService;

/**
 * 群聊自动打标签配置
 */
class ChRoomAutoTagConfigService extends AbstractService
{
    public $mapper;

    #[Inject]
    protected ChRoomMapper $roomMapper;

    #[Inject]
    protected ChRoomTaskCenterService $roomTaskCenterService;

    public function __construct(ChRoomAutoTagConfigMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * 获取配置
     * @param mixed $tagId
     * @return ChRoomAutoTagConfig|Model|string|Builder|null
     */
    public function getConfig(mixed $tagId): ChRoomAutoTagConfig|Model|string|Builder|null
    {
        return $this->mapper->findByTagId($tagId);
    }

    /**
     * 保存配置
     * @param array $data
     * @return bool|mixed
     */
    public function saveConfig(array $data): mixed
    {
        $model = $this->mapper->findByTagId($data['tag_id']);

        if(!empty($data['keywords'])){
            $data['keywords'] = json_decode($data['keywords'],true);
        }

        if (empty($model)) {
            $rst = $this->mapper->save($data);
        }else{
            $rst = $this->mapper->update($model->id,$data);
        }
        $this->mapper->delCache($data['tag_id']);
        return $rst;
    }

    /**
     * 获取满足条件的自动打群标签方案
     * @param int $mainUid
     * @param string $roomWxid
     * @param string $roomName
     * @param int $roomMemberCount
     * @return void
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function getMeetTheConditionScheme(int $mainUid,string $roomWxid,string $roomName,int $roomMemberCount): void
    {
        $roomInfo = $this->roomMapper->findByRoomWxid($roomWxid,$mainUid);

        $schemeList = $this->mapper->model::query()
            ->select(['id','uid','main_uid','tag_id','keyword_switch','keywords','member_count_switch','member_count'])
            ->where('main_uid',$mainUid)
            ->get();

        foreach ($schemeList as $scheme){
            if($this->checkScheme($scheme,$roomName,$roomMemberCount)){
                if(!$roomInfo->roomTags()->wherePivot('tag_id',$scheme->tag_id)->exists()){
                    $content = new RoomTcContentDto();
                    $content->setRoomIds([$roomInfo->id]);
                    $content->setTagIds([$scheme->tag_id]);
                    $this->roomTaskCenterService->addTask($roomInfo->uid,$roomInfo->main_uid,RoomTaskCenterTypeConstants::TAG,$content);
                }
            }
        }
    }


    private function checkScheme(ChRoomAutoTagConfig $scheme,string $roomName,int $roomMemberCount): bool
    {
        $isConditionMet = false;
        if($scheme->keyword_switch == 1 && $scheme->member_count_switch == 1){
            if(!empty($scheme->keywords) && checkKeywords($scheme->keywords,$roomName) && $roomMemberCount >= $scheme->member_count){
                //满足条件给群打标签
                $isConditionMet = true;
            }
        }elseif ($scheme->keyword_switch == 1 && $scheme->member_count_switch == 0){
            if(!empty($scheme->keywords) && checkKeywords($scheme->keywords,$roomName)){
                //满足条件给群打标签
                $isConditionMet = true;
            }
        }elseif ($scheme->keyword_switch == 0 && $scheme->member_count_switch == 1){
            if($roomMemberCount >= $scheme->member_count){
                //满足条件给群打标签
                $isConditionMet = true;
            }
        }
        return $isConditionMet;
    }
}